package com.meiwenhui.sishuwujing;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.util.NodeList;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class InjectContent {

	private static final Logger LOG = Logger.getLogger(InjectContent.class);

	String URL_PREFIX = "http://guoxue.shufaji.com/国学/";

	ComboPooledDataSource cpds = new ComboPooledDataSource();
	Connection connection = null;

	private int count;
	private int max = 500;
	private int threadCount;

	public void doing() throws Exception {
		connection = cpds.getConnection();
		PreparedStatement preparedStatement = connection.prepareStatement("select count(*) as count from home_guoxue_chapter where status =0");
		ResultSet resultSet = preparedStatement.executeQuery();
		if (resultSet.next()) {
			count = resultSet.getInt("count");
			threadCount = (count / max) + (count % max == 0 ? 0 : 1);
			for (int i = 0; i < threadCount; i++) {
				LOG.info(count + "\t" + threadCount + "\t" + (i * max) + "\t" + max);
				new Spider(i * max, max).start();
			}
		}
	}

	class Spider extends Thread {

		private int offset;
		private int limit;

		public Spider(int offset, int limit) {
			this.offset = offset;
			this.limit = limit;
		}

		public void run() {
			PreparedStatement _preparedStatement = null;
			ResultSet _resultSet = null;
			try {
				Class.forName("com.mysql.jdbc.Driver");
	            connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/sishuwujing", "root", "123456");
				_preparedStatement = connection.prepareStatement("SELECT * from home_guoxue_chapter where status =0 order by id,book asc limit " + offset + "," + limit);

				_resultSet = _preparedStatement.executeQuery();
				while (_resultSet.next()) {
					if(_resultSet.getString("book").equals("三国志") || _resultSet.getString("book").equals("后汉书"))
						continue;
					String url = URL_PREFIX + _resultSet.getString("book") + "/" + _resultSet.getString("url");
					getContent(url, _resultSet.getString("book"), _resultSet.getInt("id"));
				}
			} catch (Exception e) {
				LOG.error(e.getLocalizedMessage(), e);
			} finally {
				try {
					_preparedStatement.close();
					_resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		private void getContent(String url, String book, Integer id) {
			LOG.info(Thread.currentThread().getId() + "\t" + id + "\t" + book + "\t" + url);
			PreparedStatement __preparedStatement = null;
			PreparedStatement ___preparedStatement = null;
			try {
				String regex = "([\u4e00-\u9fa5]+)";
				Matcher matcher = Pattern.compile(regex).matcher(url);
				while (matcher.find()) {
					url = url.replace(matcher.group(0), URLEncoder.encode(matcher.group(0), "utf-8"));
				}
				URLConnection urlConnection = new URL(url).openConnection();
				String content1 = IOUtils.toString(urlConnection.getInputStream(), "utf-8");

				Parser spiderParser = new Parser();

				spiderParser.setInputHTML(content1);

				AndFilter andFilter = new AndFilter(new NodeClassFilter(Div.class), new HasAttributeFilter("align", "left"));
				NodeList chapters = spiderParser.extractAllNodesThatMatch(andFilter);
				String content = chapters.toHtml();
				content = content.substring(content.indexOf(">") + 1, content.lastIndexOf("<"));
				__preparedStatement = connection.prepareStatement("insert into home_guoxue_article values(null, ?, ?,?)");

				__preparedStatement.setString(1, book);
				__preparedStatement.setString(2, content);
				__preparedStatement.setInt(3, id);
				int effect1 = 0;
				int effect = __preparedStatement.executeUpdate();

				if (effect > 0) {
					___preparedStatement = connection.prepareStatement("update home_guoxue_chapter set status =1 where id = ?");
					___preparedStatement.setInt(1, id);
					effect1 = ___preparedStatement.executeUpdate();
				}
				LOG.info(Thread.currentThread().getId() + "\t" + id + "\t" + book + "\t" + url + "\t" + effect + "\t" + effect1);
			} catch (Exception e) {
				LOG.error("[" + id + "\t" + book + "\t" + url + "]" + e.getLocalizedMessage(), e);
			} finally {
				try {
					if (__preparedStatement != null)
						__preparedStatement.close();
					if (___preparedStatement != null)
						___preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public static void main(String[] args) {
		InjectContent injectContent = new InjectContent();
		try {
			injectContent.doing();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
